package work.timejay.user.controller.books;


import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import work.timejay.common.ApiPageEntity;
import work.timejay.common.ApiResult;
import work.timejay.user.dto.books.BookPageDTO;
import work.timejay.user.entity.books.Books;
import work.timejay.user.service.books.BooksService;
import work.timejay.user.vo.books.BookListVO;
import work.timejay.user.vo.books.BookPageVO;
import work.timejay.user.vo.books.BookTagsVO;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 书本表 前端控制器
 * </p>
 *
 * @author 王文杰
 * @since 2023-11-28
 */
@RestController
@RequestMapping("/books")
@Tag(name = "书本控制层", description = "BooksController")
public class BooksController {

    @Autowired
    private BooksService booksService;

    /**
     * 猜你喜欢列表接口
     * type 1 猜你喜欢  2 入站精选
     */
    @GetMapping
    public ApiResult<List<BookListVO>> list(@RequestParam(value = "type") Integer type) {
        List<BookListVO> voList = booksService.bookList(type);
        return ApiResult.success(voList);
    }

    /**
     * 下拉分页接口
     */
    @GetMapping(value = "/page")
    public ApiResult<ApiPageEntity<BookPageVO>> page(BookPageDTO<Books> dto) {
        ApiPageEntity<BookPageVO> iPage = booksService.bookPage(dto);
        return ApiResult.success(iPage);
    }

    /**
     * 获取点赞最多的tags书本数据
     */
    @GetMapping(value = "/tags")
    public ApiResult<List<BookTagsVO>> tags(@RequestParam(value = "num") Integer num) {
        List<BookTagsVO> voList = booksService.tags(num);
        return ApiResult.success(voList);
    }

    /**
     * 根据id查询书籍
     */
    @GetMapping(value = "/info")
    @Operation(summary = "根据id查询书籍")
    public ApiResult<Books> info(@RequestParam(value = "id") Long id) {
        Books books = booksService.findBooksById(id,true);
        return ApiResult.success(books);
    }

    /**
     * 检索数据
     */
    @GetMapping(value = "/search")
    public ApiResult<List<Books>> search(@RequestParam(value = "title") String search) {
        List<Books> books = booksService.findBooksTitle(search);
        return ApiResult.success(books);
    }

//    /**
//     * 填充数据
//     */
//    @PostMapping(value = "/batchSave")
//    @SaIgnore
//    public ApiResult<Void> batchSave(@RequestBody List<Books> books) {
//        booksService.batchSave(books);
//        return ApiResult.success();
//    }
}

